package com.bit.librarymanagementsystem.advice;


import com.bit.librarymanagementsystem.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 统一异常处理
 */

@Slf4j
@ControllerAdvice
@ResponseBody
public class ErrorAdvice {

    @ExceptionHandler//异常处理器
    public Result exceptionHandler(Exception e){
        log.error("发生异常,e",e);
        return Result.fail("内部错误, e:"+ e.getMessage());
    }

    @ExceptionHandler//异常处理器
    public Result exceptionHandler(NullPointerException e){
        log.error("发生异常,e",e);
        return Result.fail("发生空指针异常");
    }

    @ExceptionHandler//异常处理器
    public Result exceptionHandler(IndexOutOfBoundsException e){
        log.error("发生异常,e",e);
        return Result.fail("发生数组越界异常");
    }

    @ExceptionHandler//异常处理器
    public Result exceptionHandler(ArithmeticException e){
        log.error("发生异常,e",e);
        return Result.fail("发生算数异常");
    }

}
